#define _CRT_SECURE_NO_WARNINGS 1
#include<iostream>
const int N = 360, M = 41;
using namespace std;
int f[M][M][M][M], w[N], c[5];
int n, m;
int main()
{
    cin >> n >> m;
    for (int i = 1; i <= n; i++) cin >> w[i];
    for (int i = 0; i < m; i++)
    {
        int x;
        cin >> x;
        c[x]++;
    }
    f[0][0][0][0] = w[1];
    for (int A = 0; A <= c[1]; A++)
    {
        for (int B = 0; B <= c[2]; B++)
        {
            for (int C = 0; C <= c[3]; C++)
            {
                for (int D = 0; D <= c[4]; D++)
                {
                    int score = w[1 + A + 2 * B + 3 * C + 4 * D];
                    int& v = f[A][B][C][D];
                    if (A) v = max(v, f[A - 1][B][C][D] + score);
                    if (B) v = max(v, f[A][B - 1][C][D] + score);
                    if (C) v = max(v, f[A][B][C - 1][D] + score);
                    if (D) v = max(v, f[A][B][C][D - 1] + score);
                }
            }
        }
    }
    cout << f[c[1]][c[2]][c[3]][c[4]] << endl;
    return 0;
}